<?php
/**
 * 权限分组
 * Created by PhpStorm.
 * User: andy
 * Date: Date: 2019/9/2
 * Time: 下午6:34
 */

namespace app\control\controller;



use app\common\model\AdminMenuModel;
use app\common\model\AuthGroupModel;
use think\facade\Request;

class AuthController extends AdminBaseController
{
    /**
     * 后台菜单
     */
    public function index(){
        $ag=new AuthGroupModel();
        $list=$ag->getALL();

        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function edit($id=0){
        if(Request::isPost()){
            $data=Request::param();

            $AM=new AuthGroupModel();
            $status=$AM->addGroup($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=AuthGroupModel::get($id);

            $this->assign("info",$info);
            $this->assign("id",$id);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=AuthGroupModel::get($id);
        $am->status=$status;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function del(){
        $id=Request::post("id");

        $AM=new AuthGroupModel();
        $status=$AM->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(){
        $id=Request::post("ids");

        $AM=new AuthGroupModel();

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$AM->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * @param $group_id 分组id
     * @return bool|mixed
     */
    public function set($group_id){
        $info=AuthGroupModel::field("id,type_id,auth_list")->get($group_id);

        if(Request::isPost()){
            $role_id=Request::param("ids");

            if(empty($role_id)){
                $this->error("请选择需要增加的权限！");
                return false;
            }

            if(!is_array($role_id) || empty($group_id)){
                $this->error("参数异常！,code:001");
                return false;
            }

            if(empty($info)){
                $this->error("参数异常！,code:002");
                return false;
            }

            if($info->type_id!=AuthGroupModel::TYPE_ID){
                $this->error("参数异常！,code:003");
                return false;
            }

            $role_id=implode(',', $role_id);
            $info->auth_list=$role_id;
            $status=$info->save();

            if($status){
                $this->success("操作成功！");
            }else{
                $this->error("操作失败！");
            }
        }else{
            $list=AdminMenuModel::getALL()['tree'];

            $this->assign("list",$list);
            $this->assign("authList",$info->auth_list);
            $this->assign("gid",$group_id);
            return $this->fetch();
        }

    }
}